package com.cn.lg.aop;

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * Created by Administrator on 2017/8/17.
 */

public class WebLogAop {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private ThreadLocal<Long> runTime = new ThreadLocal();

    //使用配置文件形式
    public void doBefore(JoinPoint jp) {
        runTime.set(System.currentTimeMillis());
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        logger.info("URL :"+ request.getRequestURI().toString());
        logger.info("HTTP_METHOD :"+ request.getMethod());
        logger.info("IP :"+ request.getRemoteAddr());
        logger.info("ARGS :"+ Arrays.toString(jp.getArgs()));
        logger.info("log Begining method: " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());

    }

    public void doAfter(JoinPoint jp){
        logger.info("log Ending method: " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());
    }
}
